// pages/dynamic/dynamic.js
import { Dynamic } from '../../model/dynamic.js'
const dynamic = new Dynamic();
var WxParse = require("../../wxParse/wxParse.js")
import { Onlineclass } from '../../model/onlineclass.js'
const onlineclass = new Onlineclass();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    page:0,
    // 轮播
    imgUrls: [],
    indicatorDots: false,
    autoplay: true,
    interval: 3000,
    duration: 1000,
    //圆点
    swiperCurrent: 0,
    // 展示
    allmsg:{},
    // 评论
    allcount:0,
    newcount:0,
    comment_status: true,
    showTextarea: false,
    showInput:true,
    commentList: [],
    textareacontent:'',
    placehoderTxt:'说点什么...',
    // 发布评论
    replyUserName:'',
    replyUserId:'',
    commentId:'',
    // 个人信息
    userinfomation:{},
    auto_height:true,
    // loging:''
    isShow:0,
  },
  //返回
  handlerGobackClick() {
    wx.navigateBack({
        delta: 1
    })
  },

//轮播图-方法
Img_src_click(e){
  console.log(e)
  var imgArr = [];
  for (var i in this.data.imgUrls) {
    imgArr.push(this.data.imgUrls[i].picUrl)
    // if (this.data.imgUrls[i].picType != 2) {
    // imgArr.push(this.data.imgUrls[i].picUrl)
    // }
  }
  wx.previewImage({
    current: e.currentTarget.dataset.item.picUrl,
    urls: imgArr,
    success: function (res) { },
    fail: function (res) { },
    complete: function (res) { },
  })


},
swiperChange(e) {
  console.log(e)
  let current = e.detail.current;
  // console.log(current, '轮播图')
  let that = this;
  that.setData({
    swiperCurrent: current,
  })
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id
    })
    this.dynamicdDetail()
    this.commentList()
    this.usermag()
    this.getShowOrHide()
  },
  // 查看隐藏或展示
  getShowOrHide(){
    var that=this
    onlineclass.getShowOrHide().then(res=>{
      console.log(res)
      that.setData({
        isShow:res.data.isShow
      })
    }).catch(error=>{
      console.log(error)
    })
  },
  // 查看个人信息
  usermag(){
    var that = this
    wx.getStorage({
      key: 'userinfo',
      success: function(res) {
        that.setData({
          userinfomation:res.data
        })
      },
    })
  },
  
  // 查看动态详情
  dynamicdDetail(){
    var obj = {
      id:this.data.id
    }
    dynamic.dynamic_detail(obj)
    .then(res=>{
      this.setData({
        imgUrls: res.data.picList,
        allmsg: res.data.datum,

      })
      var article=res.data.datum.content;
      // console.log(article);
      WxParse.wxParse('article', 'html', article,this,1)
    })
    .catch(error=>{

    })
  },

  //评论
  textarea_input_val(e) {
    // 去前后空格
    this.setData({
      textareacontent: e.detail.value.replace(/^\s+|\s+$/g, '')
    })
  },

  // 查看评论
  commentList(){
    var obj = {
      id: this.data.id,
      page: this.data.page,
    }
    dynamic.comment_list(obj)
    .then(res=>{
      var commentList = this.data.commentList
      for (var i in res.data.datums){
        var times = res.data.datums[i].intime
        res.data.datums[i].intime = times.substring(0, 16)
        commentList.push(res.data.datums[i])
      }
      this.setData({
        commentList: commentList,
        allcount: res.data.count,
        newcount: commentList.length
      })
    })
    .catch(error=>{

    })
  },

  //发布评论
  save_comment(e){
    var params = {
      dynamicId: this.data.id,
      content: this.data.textareacontent,
    }
    //评论区域 
    if (params.content == '') {
      wx.showToast({
        title: '评论区内容不能为空',
        icon: 'none'
      })
      return false;
    }
    else if (this.data.replyUserId !== '') {
      params.replyUserId = this.data.replyUserId;
      params.commentId = this.data.commentId;
      this.Release_content(params);
    } 
    else {
      params.replyUserId = '';
      params.commentId = '';
      this.Release_content(params);
    }
  },
  //评论
  Release_content(params) {
    var that = this
    dynamic.save_comment(params)
      .then(res => {
        if (res.code == 0) {
          that.setData({
            showInput: true,
            showTextarea: false,
          })

          wx.showToast({
            title: '评论成功',
            icon: 'none'
          })
          //评论
          var entity = res.data;

          entity.replyUserNickName = that.data.replyUserName;
          entity.headPortrait = that.data.userinfomation.headPortrait;
          entity.avatar_url = that.data.userinfomation.avatarUrl;
          entity.commentUserNickName = that.data.userinfomation.nickName;
          entity.voIntime=entity.intime
          console.log(entity)
          var commentList = that.data.commentList
          var allmsg =that.data.allmsg
          commentList.push(entity);
          allmsg.commentNum = Number(allmsg.commentNum)+1 

          that.setData({
            // textareavalue: false,
            // inputvalue: true,
            textareacontent: '',
            comment_status: true,
            commentList: commentList,
            // focus_status: false,
            replyUserId:'',
            allmsg: allmsg
          })

        } else {

        }
      }).catch(error => { })
  },
  // 点击并开始评论
  input_focus(e) {
    var that = this
    that.setData({
      showInput: false,
      showTextarea: true,
      placehoderTxt: '说点什么...',
    })
  },
  
  //回复
  clickcomment(e) {
    console.log(e)
    var that = this
    var replyUserId = e.currentTarget.dataset.item.commentUserId;
    var id = e.currentTarget.dataset.item.id;
    var replyUserName = e.currentTarget.dataset.item.commentUserNickName;

    that.setData({
      showInput: false,
      showTextarea: true,
      placehoderTxt: '回复：' + replyUserName,
      replyUserName: replyUserName,
      replyUserId: replyUserId,
      commentId: id
    })
    
  }, 

  // 点赞，取消
  clickheart(){
    var num = 0
    // console.log(this.data.allmsg.isLiked)
    // return false
    if (this.data.allmsg.isLiked ==0){
      num = 1

    }
    else{
      num = 2
    }
    var obj = {
      dynamicId:this.data.id,
      type:num,
    }
    var that = this
    dynamic.like_this_dynamic(obj)
    .then(res=>{
      if (res.code == 0){
        if (num == 1){
          var allmsg = that.data.allmsg
          allmsg.likeNum = Number(allmsg.likeNum)+1
          allmsg.isLiked = 1
          that.setData({
            allmsg: allmsg
          })
        }
        else{
          var allmsg = that.data.allmsg
          allmsg.likeNum = Number(allmsg.likeNum)-1
          allmsg.isLiked = 0
          that.setData({
            allmsg: allmsg
          })
        }
      }
    })
    .catch(error=>{
      console.log(error)
    })
  },

  //投诉评论 - 删除评论
  popup(e) {
    // console.log(e)
    // return false
    var that = this;

    var commentUserId = e.currentTarget.dataset.item.commentUserId;
    var id = e.currentTarget.dataset.item.id;
    if (this.data.userinfomation.id == commentUserId) {
      wx.showActionSheet({
        itemList: ['删除'],
        success(res) {
          //删除
          if (res.tapIndex == 0) {
            that.delate(id);
          }
        },
        fail(res) {
          console.log(res.errMsg)
        }
      })
    } else {
      wx.showActionSheet({
        itemList: ['投诉'],
        success(res) {
          if (res.tapIndex == 0) {
            //投诉
            that.Complaint(id);
          }
        },
        fail(res) {
          console.log(res.errMsg)
        }
      })
    }

  },
  //删除
  delate(id) {
    var params = {
      id: id,
      userId: this.data.userinfomation.id
    }
    dynamic.delete_comment(params)
      .then(res => {
        if (res.code == 0) {

          var len = this.data.commentList.length;
          if (len == 1) {
            this.setData({
              comment_status: false
            })
          } else {
            this.setData({
              comment_status: true
            })
          }
          for (var i in this.data.commentList) {
            if (this.data.commentList[i].id == id) {
              var index = i;
              this.data.commentList.splice(index, 1)
            }
          }
          var delVar=this.data.allmsg
          delVar.commentNum=Number(delVar.commentNum)-1
          this.setData({
            commentList: this.data.commentList,
            allmsg:delVar
          })

          wx.showToast({
            title: '删除成功',
            icon: 'none'
          })


        } else {

        }

      })
      .catch(error => {

      })
  },
  //投诉
  Complaint(id) {
    wx.navigateTo({
      url: '/pages/opinion/opinion?commentId=' + id + '&type=1',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.allcount>this.data.newcount){
      this.setData({
          page:this.data.page+1
      })
      this.record()
    }
    // console.log(this.data.page)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})